'''
    寻找环形链表入口的第一个节点
'''

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
    
class Solution:
    def detectCycle(self, head: ListNode) -> ListNode:
        fast,slow = head,head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                break
            slow = head
            if not fast or not fast.next:
                return None
            while slow != fast:
                fast = fast.next
                slow = slow.next
            return slow
        